package com.anda.logistics.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.anda.logistics.map.util.JsonStatus;
import com.anda.logistics.model.SystemMenu;
import com.anda.logistics.model.SystemMenuRights;
import com.anda.logistics.model.SystemRole;
import com.anda.logistics.service.SystemMenuService;
import com.anda.logistics.service.SystemRightsService;
import com.anda.logistics.service.SystemRoleService;
import com.anda.logistics.util.JQGridJsonData;
import com.anda.logistics.util.JQGridPager;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("json-default")
public class SystemRoleAction extends AndaBaseAction implements ModelDriven<SystemRole>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 4829792764118259418L;
	private SystemRoleService systemRoleService;
	private JQGridPager pager;
	private JQGridJsonData results;
	private JsonStatus status;
	private SystemRole sr = new SystemRole();
	private SystemMenuService systemMenuService;
	private List<SystemMenu> menuList;
	private SystemRightsService systemRightsService;
	private List<SystemMenuRights> menuRightsList;
	
	@Action(value="/manager/rights/system_role"
			,results=@Result(name="success", location="system-role.jsp")
	)
	public String menuRights(){
		menuList = systemMenuService.getAll();
		menuRightsList = systemRightsService.getAll();
		return SUCCESS;
	}
	
	@Action(value="/manager/rights/system-role-list"
		,results=@Result(name="success",type="json",params={"root","results","contentType","text/html","enableGZIP","true","noCache","true"})
	)
	public String menuRightsList(){
		List<SystemRole> list = systemRoleService.getPagerList(pager);
		long records = systemRoleService.getPagerRecords(pager);
		long total = records / pager.getRows();
		if ( records % pager.getRows() > 0) total ++;
		results = new JQGridJsonData();
		results.setPage(pager.getPage());
		results.setRecords(records);
		results.setTotal(total);
		results.setRowsFromList(list,"id",new String[]{"id","roleName"});
		return SUCCESS;
	}
	
	@Action(value="/manager/rights/system-role-edit"
		,results=@Result(type="json",name="success",params={"root","status","contentType","text/html","enableGZIP","true","noCache","true"})
	)
	public String menuRightsEdit(){
		status = new JsonStatus(true);
		try {
			if (pager.getOper().equals("add")){
				systemRoleService.save(sr);
			} else if (pager.getOper().equals("edit")){
				sr.setId(Integer.parseInt(pager.getId()));
				systemRoleService.update(sr);
			} else if (pager.getOper().equals("del")){
				systemRoleService.deleteAll(pager.getId().split(","));
			}
		} catch (Exception e){
			e.printStackTrace();
			status.setMsg(e.getMessage());
			status.setSc(false);
		}
	
		return SUCCESS;
	}
	
	public SystemRoleService getSystemRoleService() {
		return systemRoleService;
	}
	@Resource
	public void setSystemRoleService(SystemRoleService systemRoleService) {
		this.systemRoleService = systemRoleService;
	}
	public SystemMenuService getSystemMenuService() {
		return systemMenuService;
	}
	@Resource
	public void setSystemMenuService(SystemMenuService systemMenuService) {
		this.systemMenuService = systemMenuService;
	}

	public SystemRightsService getSystemRightsService() {
		return systemRightsService;
	}
	@Resource
	public void setSystemRightsService(SystemRightsService systemRightsService) {
		this.systemRightsService = systemRightsService;
	}

	public List<SystemMenu> getMenuList() {
		return menuList;
	}

	public void setMenuList(List<SystemMenu> menuList) {
		this.menuList = menuList;
	}

	public JQGridPager getPager() {
		return pager;
	}
	public void setPager(JQGridPager pager) {
		this.pager = pager;
	}
	public JQGridJsonData getResults() {
		return results;
	}
	public void setResults(JQGridJsonData results) {
		this.results = results;
	}
	public JsonStatus getStatus() {
		return status;
	}
	public void setStatus(JsonStatus status) {
		this.status = status;
	}
	public SystemRole getSr() {
		return sr;
	}
	public void setSr(SystemRole sr) {
		this.sr = sr;
	}

	@Override
	public SystemRole getModel() {
		return sr;
	}

	public List<SystemMenuRights> getMenuRightsList() {
		return menuRightsList;
	}

	public void setMenuRightsList(List<SystemMenuRights> menuRightsList) {
		this.menuRightsList = menuRightsList;
	}
	
	
}
